﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using BLL;
using DLL;

namespace CuaHangBangDia
{
    public partial class Form_DuLieu : DevComponents.DotNetBar.RibbonForm
    {
        public Form_DuLieu()
        {
            InitializeComponent();
        }

        FileDialog dialog;

        private void btnDDSL_Click(object sender, EventArgs e)
        {
            dialog = new SaveFileDialog();
            dialog.Filter = "File type (*.bak)|*.bak|File type (*.*)|*.*";
            dialog.DefaultExt = "bak";
            if (dialog.ShowDialog() == DialogResult.OK)
                txtDDSL.Text = dialog.FileName;
        }

        private void btnDDPH_Click(object sender, EventArgs e)
        {
            dialog = new OpenFileDialog();
            dialog.Filter = "File type (*.bak)|*.bak|File type (*.*)|*.*";
            dialog.DefaultExt = "bak";
            if (dialog.ShowDialog() == DialogResult.OK)
                txtDDPH.Text = dialog.FileName;
        }

        private void btnSaoLuu_Click(object sender, EventArgs e)
        {
           // using (SqlConnection conn = new SqlConnection("server=.\\sqlexpress;database=CuaHangBangDia;integrated security=true"))
            if (txtDDSL.Text == "")
                MessageBox.Show("Không có đường dẫn SAO LƯU!", "Thông báo");
            else
            {
                if (!Directory.Exists(Path.GetDirectoryName(txtDDSL.Text)))
                    MessageBox.Show("Không xác định được nơi lưu trữ!");
                else
                {
                    DialogResult rs = MessageBox.Show("Bạn muốn SAO LƯU lại dữ liệu???", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (rs == DialogResult.Yes)
                    {

                        SqlConnection con = new SqlConnection("server=.\\sqlexpress;database=CuaHangBangDia;integrated security=true");
                        SqlCommand command = new SqlCommand();
                        command.CommandText = "BACKUP DATABASE CuaHangBangDia TO DISK='" + txtDDSL.Text + "'WITH FORMAT";
                        command.CommandType = CommandType.Text;
                        command.Connection = con;
                        con.Open();
                        command.ExecuteNonQuery();
                        con.Close();
                        MessageBox.Show("SAO LƯU dữ liệu thành công!");
                    }
                }
            }
        }

        private void btnPhucHoi_Click(object sender, EventArgs e)
        {
            if (txtDDPH.Text == "")
                MessageBox.Show("Không có đường dẫn PHỤC HỒI!", "Thông báo");
            else
            {
                if (!Directory.Exists(Path.GetDirectoryName(txtDDPH.Text)))
                    MessageBox.Show("Không tìm thấy file!");
                else
                {
                    DialogResult rs = MessageBox.Show("Bạn muốn PHỤC HỒI dữ liệu???", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                    if (rs == DialogResult.Yes)
                    {
                        SqlConnection con = new SqlConnection("server=.\\sqlexpress;database=CuaHangBangDia;integrated security=true");
                        SqlCommand command = new SqlCommand();
                        command.CommandText = "ALTER DATABASE CuaHangBangDia SET OFFLINE WITH ROLLBACK IMMEDIATE USE master RESTORE DATABASE CuaHangBangDia FROM DISK='" + txtDDPH.Text + "'WITH REPLACE ALTER DATABASE CuaHangBangDia SET ONLINE";
                        command.CommandType = CommandType.Text;
                        command.Connection = con;
                        con.Open();
                        command.ExecuteNonQuery();
                        con.Close();
                        MessageBox.Show("PHỤC HỒI dữ liệu thành công!");
                    }
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}